


	clear
	gen state=""
	tempfile tmp
	save `tmp'
		
	foreach state in AR AZ CA CO FL LA NV ME MI MO MT OH OK SD TX NY { 
		local files : dir "../data_output/" files "total_bills_by_sponsor_`state'*.dta"
		foreach file in `files' {
		di "`file'"
project, uses("../data_output/`file'")		
			use "../data_output/`file'", clear
			append using `tmp'
			save `tmp', replace
		}
	}

	
	
	
	
	preserve
project, uses(../data_output/first_last_run.dta)
	restore
	merge m:1 CandId using ../data_output/first_last_run.dta
	drop if _merge==2
	drop _merge
	
	drop year* s
	rename sponsor sponsored
	gen log_sponsored = log(sponsored+1)
	gen log_law = log(law+1)
	gen log_cosponsored = log(1+cosponsor)	
	bys state chamber term: egen tot = total(sponsored)
	gen pct_sponsored = 100*sponsored/tot
	drop tot
	gen year = regexs(1) if regexm(term,"-([0-9][0-9][0-9][0-9])")
	destring year, replace
	replace year = year-2
	replace year = year - 2 if state=="LA" //LA has four-year terms
	*merge in cmt data
	preserve
project, uses(../data_output/cmt_yellowbooks.dta)	
	restore
	merge 1:1 state chamber CandId year using ../data_output/cmt_yellowbooks.dta
	drop if _merge==2
	drop _merge
	*merge in chair/leader data
	preserve
project, uses(../data_output/chairs_leaders.dta)
	restore
	merge 1:1 state chamber CandId year using ../data_output/chairs_leaders.dta
	drop if _merge==2
	drop _merge	
	*merge in floor vote data
	preserve
project, uses(../data_output/floorvotes.dta)			
	restore
	merge 1:1 state  CandId term using ../data_output/floorvotes.dta
	drop if _merge==2
	drop _merge	
	*merge in committee vote data
	preserve
project, uses(../data_output/cmtvotes.dta)				
	restore
	merge 1:1 state  CandId term using ../data_output/cmtvotes.dta
	drop if _merge==2
	drop _merge		
	*merge in content data
	preserve
project, uses(../data_output/content_of_bills.dta)	
	restore
	merge 1:1 state  CandId term using ../data_output/content_of_bills.dta
	drop if _merge==2
	drop _merge

	bys state chamber term: egen total_legislators = count(CandId)
	**Andy: Did you add these 4 lines of code? we get a type mismatch error...
	gen tmp = 1 if committees!=""
	bys state chamber term: egen total_cmtmember = count(tmp)
	drop tmp	
	gen tmp = chairs!=""
	bys state chamber term: egen total_legislators_chairs = count(tmp)
	drop tmp
	gen tmp =	vicechairs!= ""
	bys state chamber term: egen total_legislators_vchairs = count(tmp)
	drop tmp
	gen tmp =	leadership!= ""	
	bys state chamber term: egen total_legislators_leadership = count(tmp)
	drop tmp

	egen chair_number = noccur(chairs), string(";") 
	replace chair_number =. if total_legislators_chairs==0
	egen vice_number = noccur(vicechairs), string(";") 
	replace vice_number =. if total_legislators_vchairs==0
	
	gen cmt_top_number = regexm(committees, "rules") + regexm(committees, "appropriat") + regexm(committees, "ways and|ways &") if total_cmtmember>0
	gen cmt_chairtop_number = regexm(chairs, "rules") + regexm(chairs, "appropriat") + regexm(chairs, "ways and|ways &") if total_legislators_chairs>0
		
	gen cmt_chair = 0 if total_legislators_chairs>0
	replace cmt_chair = 1 if chairs!=""
	gen cmt_vicechair = 0 if total_legislators_vchairs>0
	replace cmt_vicechair = 1 if vicechairs!=""
	gen leader = 0  if total_legislators_leadership>0
	replace leader=1 if leadership!="" 
	replace cmt_number = 0 if cmt_number==. & total_cmtmember>0
	gen cmt_top = 0 if total_cmtmember>0
	replace cmt_top = 1 if regexm(committees, "approp|ways and means|ways & means|rules")	
	gen cmt_index = cmt_number + vice_number + 2* chair_number  //+ cmt_top_number +
		
	
	** time fixed effects
	egen dist = group( state chamber district),  label
	egen dist_term = group( state chamber district term),  label
	bys chamber CandId: egen cohort = min(year)
	egen cohort_term = group(cohort term),  label
	egen state_chamber = group(state chamber) ,  label
	egen chamber_term = group(state chamber term) ,  label
	egen party_chamber_term = group(state party chamber term) ,  label
	gen log_cmtvotes = log(cmtvotes + 1)
	gen terms_in_office_sq = terms_in_office^2
	
	gen lifelimit = regexm(state, "MI|MO|CA|OK|NV|AR")
	gen lifelimitXtermlimited = termlimited*lifelimit
	gen run_again_same = [last_run_house>year & chamber=="house"] | [last_run_senate>year & chamber=="senate"]
	gen run_again_other = [last_run_house>year & chamber=="senate"] | [last_run_senate>year & chamber=="house"]
	gen never_run_again = 1-run_again_same
	gen never_run_againXtermlimited = never_run_again*termlimited
	replace vtsh=vtsh/100
	gen vtshXtermlimited = vtsh*termlimited
	
	gen next_election = regexs(1) if regexm(term, "-([0-9]+)")
	destring next_election, replace
	gen last_election = next_election-2
	replace last_election = next_election-4 if state=="LA"	
	
	gen permitted_terms = .
	replace permitted_terms = 3 if regexm(state, "MI|CA|LA")
	replace permitted_terms = 4 if regexm(state, "AZ|CO|FL|ME|MT|OH|SD|MO")
	replace permitted_terms = 6 if regexm(state, "NV|OK")
	replace permitted_terms = 8 if regexm(state, "AR") 
	gen termlimits_effective = .
	replace termlimits_effective = 2010 if regexm(state, "NV")
	replace termlimits_effective = 2004 if regexm(state, "OK")
	replace termlimits_effective = 2002 if regexm(state, "MO")
	replace termlimits_effective = 2000 if regexm(state, "AZ|MT|SD|OH|FL")
	replace termlimits_effective = 1998 if regexm(state, "MI|AR|CO")
	replace termlimits_effective = 1996 if regexm(state, "CA|ME")
	replace termlimits_effective = 2007 if regexm(state, "LA")	
	gen termlimits_effective_dummy = next_election>=termlimits_effective
	gen TermLimits_prev = next_election>termlimits_effective
		
	gen termlimits_enacted = .
	replace termlimits_enacted = 1993 if regexm(state, "ME")
	replace termlimits_enacted = 1990 if regexm(state, "CA|CO|OK")
	replace termlimits_enacted = 1992 if regexm(state, "AR|MI|FL|OH|SD|MT|AZ|MO")
	replace termlimits_enacted = 1995 if regexm(state, "LA")
	replace termlimits_enacted = 1996 if regexm(state, "NV")
	gen termlimits_enacted_dummy = next_election>=termlimits_enacted
	bys CandId: egen termlimited_type = max(termlimited)
		
	xtset CandId
	sort chamber CandId term
	by chamber CandId : egen max_terms_in_office = max(terms_in_office)

	*[cmt_top + cmt_chair + (/max_cmt_number) ] / 3
				
	gen log_symbolic = log(symbolic+1)
	gen log_fiscal = log(fiscal+1)
	
	gen symbolic_share = symbolic / tot_bills_content
	gen vtsh_2 = vtsh^2

	
	*******HERE WE DROP DATA BEFORE TERMLIMITS WERE PASSED*****
	*keep if termlimits_enacted_dummy==1
	
	pca log_sponsored pct_floorvotes cmt_index
	predict pc1, score
	sum pc1
	replace pc1 = [pc1 - r(mean) ]/ r(sd) 
	

	gen first_year_elected = regexs(1) if regexm(elections_won, "^([0-9]+);")
	destring first_year_elected, replace
	
	sort chamber CandId term
	foreach v in cmt_index log_sponsored pc1 cmt_number pct_floorvotes cmt_chair cmt_vice log_cmtvotes log_cosponsor log_law cmt_top {
			gen first_`v' = `v' if first_year_elected==year
	}	
			

	preserve
project , uses(../data_output/idealpoints.dta)	
	restore
	merge 1:1 CandId year using ../data_output/idealpoints.dta
	drop if _merge==2
	drop _merge
				
	replace vote_party_line = 100*vote_party_line
	replace vote_against_party = 100*vote_against_party
	
	save ../data_output/termlimited.dta, replace
	
	*merge in interest group ratings
	preserve
project , uses("../data_output/ratings.dta")	
	restore
	
	use ../data_output/termlimited.dta	, clear
	
	merge 1:1 CandId term using "../data_output/ratings.dta"
	drop if _merge==2
	drop _merge
	
	save ../data_output/termlimited.dta, replace

project , uses("../data_output/stleg_prof.dta")	

	use ../data_output/termlimited.dta	, clear	
	merge m:1 state year using "../data_output/stleg_prof.dta"
	drop if _merge==2
	drop _merge
	save ../data_output/termlimited.dta, replace
	
	
		
	egen terms_in_officeXchamber = group(state chamber terms_in_office)
	egen chamber_party_term = group(state chamber party term)
	
	
		
	save ../data_output/termlimited_includenontermlimitstates.dta, replace

	
	*drop states without term limits
	
	drop if regexm(state, "TX|NY")
	
	
	save ../data_output/termlimited.dta, replace
	
	save "../data_output/termlimited_tmp.dta", replace
project, creates(../data_output/termlimited_tmp.dta)	
	
project, creates(../data_output/termlimited_includenontermlimitstates.dta)	
	
	
	
	
